package com.cdu.supermarket.controller.good;


import com.cdu.supermarket.common.web.response.JsonResult;
import com.cdu.supermarket.entity.domain.GoodsAttribute;
import com.cdu.supermarket.entity.domain.GoodsAttributeOption;
import com.cdu.supermarket.service.IGoodsAttributeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品属性控制器
 */
@RestController
@Validated
@RequestMapping("/goods_management/attribute")
public class GoodsAttributeController {

    @Resource
    private IGoodsAttributeService attributeService;

    /**
     * 获取所有商品属性
     */
    @GetMapping("/list")
    public JsonResult list() {
        List<GoodsAttribute> attributes = attributeService.list();
        
        // 为枚举类型的属性加载选项
        if (attributes != null && !attributes.isEmpty()) {
            for (GoodsAttribute attribute : attributes) {
                if ("enum".equals(attribute.getAttrType())) {
                    List<GoodsAttributeOption> options = attributeService.getAttributeOptions(attribute.getId());
                    attribute.setOptions(options);
                }
            }
        }
        
        return JsonResult.success(attributes);
    }

    /**
     * 根据分类ID获取属性
     */
    @GetMapping("/listByCategoryId")
    public JsonResult listByCategoryId(@NotNull(message = "分类ID不能为空") Long categoryId) {
        return JsonResult.success(attributeService.listByCategoryId(categoryId));
    }

    /**
     * 获取属性详情
     */
    @GetMapping("/getById")
    public JsonResult getById(@NotNull(message = "属性ID不能为空") Long id) {
        GoodsAttribute attribute = attributeService.getById(id);
        
        // 如果是枚举类型，加载选项列表
        if (attribute != null && "enum".equals(attribute.getAttrType())) {
            List<GoodsAttributeOption> options = attributeService.getAttributeOptions(id);
            attribute.setOptions(options);
        }
        
        return JsonResult.success(attribute);
    }

    /**
     * 保存属性
     */
    @PostMapping("/save")
    public JsonResult save(@RequestBody GoodsAttribute attribute) {
        boolean success = attributeService.save(attribute);
        
        // 如果有选项且是枚举类型，保存选项
        if (success && "enum".equals(attribute.getAttrType()) && 
            attribute.getOptions() != null && !attribute.getOptions().isEmpty()) {
            List<String> optionValues = attribute.getOptions().stream()
                .map(option -> option.getOptionValue())
                .collect(Collectors.toList());
            attributeService.saveBatchOptions(attribute.getId(), optionValues);
        }
        
        return JsonResult.success();
    }

    /**
     * 更新属性
     */
    @PostMapping("/update")
    public JsonResult update(@RequestBody GoodsAttribute attribute) {
        boolean success = attributeService.updateById(attribute);
        
        // 如果有选项且是枚举类型，更新选项
        if (success && "enum".equals(attribute.getAttrType()) && 
            attribute.getOptions() != null && !attribute.getOptions().isEmpty()) {
            List<String> optionValues = attribute.getOptions().stream()
                .map(option -> option.getOptionValue())
                .collect(Collectors.toList());
            attributeService.saveBatchOptions(attribute.getId(), optionValues);
        }
        
        return JsonResult.success();
    }

    /**
     * 删除属性
     */
    @PostMapping("/delete")
    public JsonResult delete(@NotNull(message = "属性ID不能为空") Long id) {
        attributeService.removeById(id);
        return JsonResult.success();
    }

    /**
     * 获取属性选项
     */
    @GetMapping("/getOptions")
    public JsonResult getOptions(@NotNull(message = "属性ID不能为空") Long attrId) {
        List<GoodsAttributeOption> options = attributeService.getAttributeOptions(attrId);
        return JsonResult.success(options);
    }
} 